<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('外派人员') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('申请标题')" prop="actTitle">
        <el-input
          v-model="queryParams.actTitle"
          :placeholder="$t('申请标题')"
          size="small"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('姓名')" prop="user.userId">
        <UserSelect
          v-model="queryParams.user.userId"
          :placeholder="$t('姓名')"
        />
      </el-form-item>
      <el-form-item :label="$t('外派职位')" prop="level">
        <el-select
          v-if="this.$route.query.dealType === 'bf'"
          v-model="queryParams.level"
          :placeholder="$t('外派职位')"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in appointOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
        <el-select v-else v-model="queryParams.level" :placeholder="$t('外派职位')" clearable size="small">
          <el-option
            v-for="dict in levelOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('外派时间')" prop="expatsTime">
        <DatePicker v-model="expatsTimes" type="daterange" />
      </el-form-item>
      <el-form-item :label="$t('外派地点')" prop="expatsAddress">
        <el-input
          v-model="queryParams.expatsAddress"
          :placeholder="$t('外派地点')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:expats:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:expats:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealExpatsList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请标题')" align="center" prop="actTitle" :show-overflow-tooltip="true" min-width="200">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.actTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('姓名')" align="center" prop="user.nickName" show-overflow-tooltip width="90" />
      <el-table-column
        v-if="this.$route.query.dealType === 'bf'"
        :label="$t('外派职位')"
        align="center"
        prop="level"
        :formatter="appointFormat"
        width="90"
        show-overflow-tooltip
      />
      <el-table-column
        v-else
        :label="$t('外派职位')"
        align="center"
        prop="level"
        :formatter="levelFormat"
        width="90"
        show-overflow-tooltip
      />
      <el-table-column :label="$t('外派时间')" align="center" prop="expatsTime" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expatsTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('外派地点')" align="center" prop="expatsAddress" show-overflow-tooltip />
      <el-table-column :label="$t('外派结束时间')" align="center" prop="expatsEndTime" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expatsEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('是否结束')" align="center" prop="finish" :formatter="finishFormat" width="100" />
      <el-table-column :label="$t('说明')" align="center" prop="explain" show-overflow-tooltip />
      <el-table-column :label="$t('审批状态')" align="center" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" min-width="200">
        <template slot-scope="scope">
          <!-- 启动流程，终止流程，修改，删除 四个按钮 -->
          <ListBtn :bus-data="scope.row" has-permi="deal:expats" />
          <el-button
            v-if="scope.row.actStatus !== '' && scope.row.finish === 'N'"
            v-hasPermi="['deal:expats:edit']"
            size="small"
            type="text"
            icon="el-icon-video-pause"
            @click="updateExpatsEndTime(scope.row.id)"
          >{{ $t("结束外派") }}
          </el-button>
        </template>

      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改外派人员对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <dealExpatsForm ref="dealExpatsForm" />
    </el-dialog>
    <el-dialog
      :title="$t('结束外派')"
      :visible.sync="endOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      width="30%"
      custom-class="dialogNew"
      append-to-body
    >
      <el-form ref="endTimeForm" :model="endTimeForm" :rules="rules" label-width="80px" class="dataForm">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('外派结束时间')" prop="expatsEndTime">
              <DatePicker v-model="endTimeForm.expatsEndTime" :placeholder="$t('外派结束时间')" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-otherBtnHide" style="text-align: right;">
        <el-button @click="cancelEndTimeForm()">{{ $t("取消") }}</el-button>
        <el-button type="primary" @click="submitEndTimeForm()">{{ $t("保存") }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDealExpats, delDealExpats, saveExpatsEndTime } from '@/api/deal/expats/dealExpats'
import dealExpatsForm from './dealExpatsForm.vue'
import ProcessStatus from '@/components/Act/ProcessStatus/index'

export default {
  components: {
    dealExpatsForm,
    ProcessStatus
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 外派人员表格数据
      dealExpatsList: [],
      // 外派时间 参数
      expatsTimes: [],
      // 弹出层标题
      title: '外派人员',
      // 是否显示弹出层
      formOpen: false,
      // 外派结束时间弹出层
      endOpen: false,
      // 是否隐藏保存按钮
      btnHide: false,
      // 是否隐藏其他按钮
      otherBtnHide: false,
      // 级别 字典deal_level_type字典 债权投资使用
      levelOptions: [],
      // 级别 字典deal_level_type字典 参股基金使用
      appointOptions: [],
      // 是否结束 字典 sys_yes_no字典
      finishOptions: [],
      // 查询参数
      queryParams: {
        user: {
          userId: undefined
        },
        dealId: undefined,
        companyId: this.$route.query.companyId,
        level: undefined,
        expatsTime: undefined,
        expatsAddress: undefined,
        actId: undefined,
        actStatus: undefined,
        actTitle: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      endTimeForm: {
        id: undefined,
        expatsEndTime: undefined
      },
      // 表单校验
      rules: {
        expatsEndTime: [
          { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('deal_level_type').then(response => {
      this.levelOptions = response.data
    })
    this.getDicts('appoint_position').then(response => {
      this.appointOptions = response.data
    })
    this.getDicts('sys_yes_no').then(response => {
      this.finishOptions = response.data
    })
  },
  methods: {
    /** 查询外派人员列表 */
    getList() {
      this.loading = true
      // 时间参数赋值
      if (this.expatsTimes != null && this.expatsTimes.length > 1) {
        this.queryParams.startDate = this.expatsTimes[0]
        this.queryParams.endDate = this.expatsTimes[1]
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
      listDealExpats(this.queryParams).then(response => {
        this.dealExpatsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 级别 字典deal_level_type字典翻译 债权投资使用
    levelFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.levelOptions, row.level)
    },
    // 级别 字典deal_level_type字典翻译  参股基金使用
    appointFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.appointOptions, row.level)
    },
    // 是否结束 字典 sys_yes_no字典翻译
    finishFormat(row) {
      return this.selectDictLabel(this.finishOptions, row.finish)
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        user: {
          userId: ''
        },
        dealId: this.$route.query.id,
        level: undefined,
        expatsTime: undefined,
        expatsAddress: undefined,
        explain: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        actId: undefined,
        actStatus: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.expatsTimes = undefined
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealExpatsForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealExpatsForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delDealExpats(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcelPost('/expats/dealExpats/export', this.queryParams)
    },
    /** 结束外派按钮操作 */
    updateExpatsEndTime(id) {
      this.endOpen = true
      // debugger
      this.initEndTime(id)
    },
    // 初始化数据
    initEndTime(id) {
      this.endTimeForm.id = id
      this.endTimeForm.expatsEndTime = new Date()
    },
    // 结束外派
    submitEndTimeForm() {
      this.$refs['endTimeForm'].validate(valid => {
        // 验证数据
        if (valid) {
          saveExpatsEndTime(this.endTimeForm).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeEndTimeForm()
            } else {
              this.msgError(response.msg)
            }
            this.endOpen = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    // 关闭结束外派页面
    closeEndTimeForm() {
      // 关闭form页面
      this.endOpen = false
      // 刷新list页面
      this.getList()
    },
    // 取消按钮
    cancelEndTimeForm() {
      this.endOpen = false
    }
  }
}
</script>
